<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Create A Form - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/learning.quickstart.create-form.html">Inglês (English)</a></li>
    <li><a href="../pt-br/learning.quickstart.create-form.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.quickstart.create-model.html">Create a Model and Database Table</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.quickstart.html">Guia R&aacute;pido do Zend Framework</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.quickstart.conclusion.html">Congratulations!</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="learning.quickstart.create-form" class="section"><div class="info"><h1 class="title">Create A Form</h1></div>
    

    <p class="para">
        For our guestbook to be useful, we need a form for submitting new entries.
    </p>

    <p class="para">
        Our first order of business is to create the actual form class. To create the empty form
        class, execute:
    </p>

    <pre class="programlisting brush: shell">
% zf create form Guestbook
Creating a form at application/forms/Guestbook.php
Updating project profile &#039;.zfproject.xml&#039;
</pre>


    <p class="para">
        This will create the directory <var class="filename">application/forms/</var> with the classfile
        <var class="filename">Guestbook.php</var>. Open that file and update it so it reads as follows:
    </p>

    <pre class="programlisting brush: php">
// application/forms/Guestbook.php

class Application_Form_Guestbook extends Zend_Form
{
    public function init()
    {
        // Set the method for the display form to POST
        $this-&gt;setMethod(&#039;post&#039;);

        // Add an email element
        $this-&gt;addElement(&#039;text&#039;, &#039;email&#039;, array(
            &#039;label&#039;      =&gt; &#039;Your email address:&#039;,
            &#039;required&#039;   =&gt; true,
            &#039;filters&#039;    =&gt; array(&#039;StringTrim&#039;),
            &#039;validators&#039; =&gt; array(
                &#039;EmailAddress&#039;,
            )
        ));

        // Add the comment element
        $this-&gt;addElement(&#039;textarea&#039;, &#039;comment&#039;, array(
            &#039;label&#039;      =&gt; &#039;Please Comment:&#039;,
            &#039;required&#039;   =&gt; true,
            &#039;validators&#039; =&gt; array(
                array(&#039;validator&#039; =&gt; &#039;StringLength&#039;, &#039;options&#039; =&gt; array(0, 20))
                )
        ));

        // Add a captcha
        $this-&gt;addElement(&#039;captcha&#039;, &#039;captcha&#039;, array(
            &#039;label&#039;      =&gt; &#039;Please enter the 5 letters displayed below:&#039;,
            &#039;required&#039;   =&gt; true,
            &#039;captcha&#039;    =&gt; array(
                &#039;captcha&#039; =&gt; &#039;Figlet&#039;,
                &#039;wordLen&#039; =&gt; 5,
                &#039;timeout&#039; =&gt; 300
            )
        ));

        // Add the submit button
        $this-&gt;addElement(&#039;submit&#039;, &#039;submit&#039;, array(
            &#039;ignore&#039;   =&gt; true,
            &#039;label&#039;    =&gt; &#039;Sign Guestbook&#039;,
        ));

        // And finally add some CSRF protection
        $this-&gt;addElement(&#039;hash&#039;, &#039;csrf&#039;, array(
            &#039;ignore&#039; =&gt; true,
        ));
    }
}
</pre>


    <p class="para">
        The above form defines five elements: an email address field, a comment field, a
        <acronym class="acronym">CAPTCHA</acronym> for preventing spam submissions, a submit button, and a
        <acronym class="acronym">CSRF</acronym> protection token.
    </p>

    <p class="para">
        Next, we will add a  <span class="methodname">signAction()</span> to our
        <span class="classname">GuestbookController</span> which will process the form upon submission. To
        create the action and related view script, execute the following:
    </p>

    <pre class="programlisting brush: shell">
% zf create action sign Guestbook
Creating an action named sign inside controller
    at application/controllers/GuestbookController.php
Updating project profile &#039;.zfproject.xml&#039;
Creating a view script for the sign action method
    at application/views/scripts/guestbook/sign.phtml
Updating project profile &#039;.zfproject.xml&#039;
</pre>


    <p class="para">
        As you can see from the output, this will create a  <span class="methodname">signAction()</span>
        method in our controller, as well as the appropriate view script.
    </p>

    <p class="para">
        Let&#039;s add some logic into our guestbook controller&#039;s sign action. We need to first check if
        we&#039;re getting a <acronym class="acronym">POST</acronym> or a <acronym class="acronym">GET</acronym> request; in the latter
        case, we&#039;ll simply display the form. However, if we get a <acronym class="acronym">POST</acronym> request,
        we&#039;ll want to validate the posted data against our form, and, if valid, create a new entry
        and save it. The logic might look like this:
    </p>

    <pre class="programlisting brush: php">
// application/controllers/GuestbookController.php

class GuestbookController extends Zend_Controller_Action
{
    // snipping indexAction()...

    public function signAction()
    {
        $request = $this-&gt;getRequest();
        $form    = new Application_Form_Guestbook();

        if ($this-&gt;getRequest()-&gt;isPost()) {
            if ($form-&gt;isValid($request-&gt;getPost())) {
                $comment = new Application_Model_Guestbook($form-&gt;getValues());
                $mapper  = new Application_Model_GuestbookMapper();
                $mapper-&gt;save($comment);
                return $this-&gt;_helper-&gt;redirector(&#039;index&#039;);
            }
        }

        $this-&gt;view-&gt;form = $form;
    }
}
</pre>


    <p class="para">
        Of course, we also need to edit the view script; edit
        <var class="filename">application/views/scripts/guestbook/sign.phtml</var> to read:
    </p>

    <pre class="programlisting brush: php">
&lt;!-- application/views/scripts/guestbook/sign.phtml --&gt;

Please use the form below to sign our guestbook!

&lt;?php
$this-&gt;form-&gt;setAction($this-&gt;url());
echo $this-&gt;form;
</pre>


    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Better Looking Forms</b><br /></span>
        

        <p class="para">
            No one will be waxing poetic about the beauty of this form anytime soon. No matter -
            form appearance is fully customizable! See the <a href="zend.form.decorators.html" class="link">decorators section in the reference guide</a>
            for details.
        </p>

        <p class="para">
            Additionally, you may be interested in <a href="learning.form.decorators.intro.html" class="link">our tutorial on form decorators</a>.
        </p>
    </p></blockquote>

    <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Checkpoint</b><br /></span>
        

        <p class="para">
            Now browse to &quot;http://localhost/guestbook/sign&quot;. You should see the following in your
            browser:
        </p>

        <p class="para">
            <div class="inlinemediaobject"><div class="imageobject"><img src="images/d481d625821a97b9a5eb2cec99dca50e-learning.quickstart.create-form.png" alt="learning.quickstart.create-form.png" width="421" height="555" /></div></div>
        </p>
    </p></blockquote>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="learning.quickstart.create-model.html">Create a Model and Database Table</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="learning.quickstart.html">Guia R&aacute;pido do Zend Framework</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="learning.quickstart.conclusion.html">Congratulations!</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="learning.html">Conhecendo o Zend Framework</a></li>
  <li class="header up"><a href="learning.quickstart.html">Guia R&aacute;pido do Zend Framework</a></li>
  <li><a href="learning.quickstart.intro.html">Introdu&ccedil;&atilde;o ao Zend Framework e ao MVC</a></li>
  <li><a href="learning.quickstart.create-project.html">Crie Seu Projeto</a></li>
  <li><a href="learning.quickstart.create-layout.html">Create A Layout</a></li>
  <li><a href="learning.quickstart.create-model.html">Create a Model and Database Table</a></li>
  <li class="active"><a href="learning.quickstart.create-form.html">Create A Form</a></li>
  <li><a href="learning.quickstart.conclusion.html">Congratulations!</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>